// pages/classDetail/classDetail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detail: 0, // 0-详情, 1-评论
    // showBuy: false, // 展示买弹窗
    productInfo:{},
  

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
          url: 'https://bangkeedu.com/api/product/getProductById', 
          method:'get',
          data: {
            id:options.id
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            that.setData({
              productInfo:res.data,
           

            })
           //  console.log(that.data.productInfo)
          }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changTab: function (e) {
    const detail = Number(e.currentTarget.dataset.value)
    this.setData({
      detail: detail
    })
  },
  immBuy: function() {
    let that = this;
    if (getApp().globalData.userInfo.openid) {
      wx.request({
        url:'https://bangkeedu.com/api/pay/applets',
        method:'GET',
        header:{
          'Authorization': 'Bearer ' + getApp().globalData.userInfo?.token
        },
        data:{
          openid:getApp().globalData.userInfo.openid,
          description:this.data.productInfo.product_description,
          price:this.data.productInfo.product_price,
          product:this.data.productInfo.id
        },
        success(res){
          if (res.data) {
            if (res.data.pay_state == 'SUCCESS') {//已支付跳转我的视频
              console.log('已支付跳转我的视频');
              wx.redirectTo({
                url: '/pages/coursescenter/coursesceter',
                success(){
                  wx.showToast({
                    title: '请勿重复购买！',
                    icon: 'success',
                    duration: 3000
                  })
                }
              })
            }else{
              wx.requestPayment({
                "timeStamp": res.data.timeStamp,
                "nonceStr": res.data.nonceStr,
                "package": res.data.package,
                "signType": res.data.signType,
                "paySign": res.data.paySign,
                "success":function(res){//支付成功跳转我的视频
                  // console.log('success: ',res.errMsg);
                  wx.redirectTo({
                    url: '/pages/coursescenter/coursesceter',
                    success(){
                      wx.showToast({
                        title: '购买成功！',
                        icon: 'success',
                        duration: 3000
                      })
                    }
                  })
                }
              })
            }
            // const {nonceStr,package,paySign,signType,timeStamp} = res.data
            
          }
        }
      })
    }else{//跳转登录
      wx.showModal({
        title: '提示',
        content: '请登录后购买！',
        success: function (res) {
          if (res.confirm) {//这里是点击了确定以后
            wx.switchTab({
              url: '/pages/my/my'
            })
          }
        }
      })
    }
  },
  // closeBuy: function() {
  //   this.setData({
  //     showBuy: false,
  //   })
  // }
})